<template>
	<view class="login-box">
		<view
			style="width: 100%;height: 95%;display: flex;justify-content: center;align-items: center;flex-direction: column;">
			<image src="../../static/hsst.png" style="height: 45px;width: 90%;"></image>
			<view
				style="border-radius: 5px;background-color: white;display: flex;flex-direction: row;align-items: center;height: 50px;width: 90%;margin-top: 20px;">
				<image src="../../static/user.png" style="width: 25px;height: 25px;margin-left: 10px;"></image>
				<input
					style="-webkit-user-select: auto;caret-color: #20A2F1;color: #444444;font-size: 15px;margin-left: 10px;flex: 1;"
					placeholder="请输入用户名" v-model="userName" />
			</view>

			<view
				style="border-radius: 5px;background-color: white;display: flex;flex-direction: row;align-items: center;height: 50px;width: 90%; margin-top: 20px;">
				<image src="../../static/password.png" style="width: 25px;height: 25px;margin-left: 10px;"></image>
				<input
					style="-webkit-user-select: auto;caret-color: #20A2F1;color: #44444;font-size: 15px;margin-left: 10px;flex: 1;"
					placeholder="请输入密码" v-model="passWord" :password="ispass" />
				<image :src='iconPass' style="width: 60px;height: 100%;" @click="hide"></image>
			</view>

			<button style="background-color: #1AC7A2;margin-top: 30px;color: white;width: 90%;"
				@click="login">登录</button>


			<checkbox-group @change="selectCk" style="width: 90%;display: flex;flex-direction: row;margin-top: 25px;">
				<checkbox style="color: white;" class="round red" value="记住密码" :checked="isPassWord">记住密码</checkbox>
				<view style="flex: 1;"></view>
				<checkbox style="color: white;" value="自动登录" :checked="isLogin">自动登录</checkbox>
			</checkbox-group>

		</view>
		<text
			style="color: white;font-size: 12px;width: 100%;display: flex; justify-content: center;">@版权所有：和一汇盛科技发展有限责任公司</text>
	</view>
</template>

<script>
	import {
		rsaEncrypt,
		rsaDecrypt
	} from '../../comment/rsa.js'

	export default {
		data() {
			return {
				userName: '',
				passWord: '',
				namesel: '',
				isPassWord: false,
				isLogin: false,
				ispass: true,
				iconPass: '../../static/hide.png',


			}
		},

		onLoad() {
			var that = this
			if (uni.getStorageSync('isPassWord') != '') {
				that.isPassWord = uni.getStorageSync('isPassWord')
			}
			if (uni.getStorageSync('isLogin') != '') {
				that.isLogin = uni.getStorageSync('isLogin')
			}

			console.log('111111111', uni.getStorageSync('isPassWord'), uni.getStorageSync('isLogin'))
			if (that.isPassWord) {
				that.userName = uni.getStorageSync('userName')
				that.passWord = uni.getStorageSync('passWord')
			}

			if (that.isLogin) {
				that.login()
			}
		},

		methods: {

			selectCk(e) {
				let that = this
				console.log('eee', e)
				that.namesel = e.detail.value //定义namesel获取复选框的数组值
				console.log('namesel的值', that.namesel)
				if (that.namesel.indexOf('记住密码') != -1) {
					uni.setStorageSync('isPassWord', true)
				} else {
					uni.setStorageSync('isPassWord', false)
				}

				if (that.namesel.indexOf('自动登录') != -1) {
					uni.setStorageSync('isLogin', true)
				} else {
					uni.setStorageSync('isLogin', false)
				}
			},


			login() {

				if (this.userName == '' || this.passWord == '') {
					uni.showToast({
						title: '用户名密码不能为空！',
						icon: 'none'
					})
					return
				}

				// 明文
				let msg = this.passWord
				// 加密后的密文
				let encryptMsg = rsaEncrypt(msg)
				console.log(encryptMsg)
				// // 解密后的明文
				// let decryptMsg = rsaDecrypt(encryptMsg)
				// console.log(decryptMsg)
				uni.showLoading({
					title: '正在登录...'
				})
				uni.request({
					url: this.$baseURL + 'auth/login',
					dataType: 'json',
					data: {
						"username": this.userName,
						"password": encryptMsg,
						"loginType": "Android"
					},
					method: "POST",
					header: {
						'content-type': 'application/json', //自定义请求头信息
					},
					success: (res) => {
						uni.hideLoading()
						console.log("请求成功", res.data);
						if (res.data.code == 200) {
							uni.setStorageSync('token', res.data.data.token)
							if (this.namesel.indexOf('记住密码') != -1) {
								uni.setStorageSync('userName', this.userName)
								uni.setStorageSync('passWord', this.passWord)
							}
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							})
							uni.switchTab({
								url: '/pages/index/index'
							})
						} else if (res.data.code == 500) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '登录失败',
								icon: 'error'
							})
						}
					},

					fail: (res) => {
						uni.hideLoading()
						uni.showToast({
							title: '请求接口失败',
							icon: 'error'
						})
						console.log(res);
					}
				});
			},

			hide() {
				if (this.ispass) {
					this.iconPass = '../../static/visible.png'
					this.ispass = false
				} else {
					this.iconPass = '../../static/hide.png'
					this.ispass = true
				}
			}
		}
	}
</script>

<style scoped>
	.login-box {
		width: 100vw;
		height: 100vh;
		background-image: url('~@/static/bg_login.png');
		background-size: 100% 100%;
	}
</style>